<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Auth;

class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(Request $request)
    {
        switch ($this->method()) {
            case 'POST':
                $rules =  [
                    'name' => 'required|string|min:2|max:20|regex:/^[A-Za-z0-9\-\_]+$/',
                    'name' =>[Rule::unique('users')->whereNull('deleted_at')],
                    'section_id' => 'nullable|integer|exists:roles,id',
                    'branch_id' => 'nullable|integer|exists:roles,id',
                    'role_id' => 'nullable|integer|exists:roles,id',
                ];
                break;
            case 'PATCH':

                if(!empty($this->reset)){
                    $rules = [
                    ];
                }elseif (isset($this->status)){
                    $rules = [
                        'status'=> Rule::in([0, 1]),
                    ];
                }
                else{
                    $rules = [
                        'name' => 'required|string|min:2|max:20|regex:/^[A-Za-z0-9\-\_]+$/',
                        'name' =>[Rule::unique('users')->whereNot('id',$request->route('user')->id)->whereNull('deleted_at')],
                        'section_id' => 'nullable|integer|exists:roles,id',
                        'branch_id' => 'nullable|integer|exists:roles,id',
                        'role_id' => 'nullable|integer|exists:roles,id',
                    ];
                }
        }

        return $rules;
    }

    public function attributes()
    {
        return [
            'section_id' => '部门',
            'branch_id' => '部门分支',
            'role_id' => '岗位',
        ];
    }
}
